/**
 ****************************************************************************
 * Copyright (C) Marcelo F. Ochoa. All rights reserved.                      *
 * ------------------------------------------------------------------------- *
 * This software is published under the terms of the Apache Software License *
 * version 1.1, a copy of which has been included  with this distribution in *
 * the LICENSE file.                                                         *
 */

package com.prism.toolkit;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.SQLException;

/* Wrapper class for multi valued form data type.
 * used when declare for example
 * <input type=hidden name=h_t value=xx>
 * <input type=hidden name=h_t value=yy>
 * Procedures which handle this argument must be declared as
 * public void proc(byte []h_t,....)
 * then this argument could by converted from byte[] to JowaArray
 * by using the provides constructor.
 */

public class JowaArray {
    String[] buff;
    int size;
    public static final int DEFAULT_SIZE = 255;

    public JowaArray() {
        buff = new String[DEFAULT_SIZE];
        size = 0;
    }

    public JowaArray(int size_t) {
        buff = new String[size_t];
        size = size_t;
    }

    public JowaArray(String[] pt) {
        size = pt.length;
        buff = new String[size];
        for (int i = 0; i < size; i++)
            buff[i] = pt[i];
    }

    public JowaArray(byte[] st) throws SQLException {
        try {
            ByteArrayInputStream i = new ByteArrayInputStream(st);
            ObjectInputStream in = new ObjectInputStream(i);
            int size_t = in.readInt();
            size = size_t;
            buff = new String[size];
            for (int j = 0; j < size_t; j++)
                buff[j] = (String)in.readObject();
        } catch (Exception e) {
            throw new SQLException(e.getMessage());
        }
    }

    public byte[] getBytes() throws SQLException {
        try {
            ByteArrayOutputStream o = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(o);
            out.writeInt(size);
            for (int j = 0; j < buff.length; j++)
                out.writeObject(buff[j]);
            out.flush();
            return o.toByteArray();
        } catch (Exception e) {
            throw new SQLException(e.getMessage());
        }
    }

    public void setValueAt(int pos, String s) {
        buff[pos] = s;
    }

    public String getValueAt(int pos) {
        return buff[pos];
    }

    public String toString() {
      StringBuffer outb = new StringBuffer("[");
      for (int j = 0; j < buff.length; j++)
        outb.append(buff[j]).append(",");
      outb = new StringBuffer(outb.toString().substring(0, outb.length() - 1));
      if (outb.length() > 0)
         outb.append("]");
      return outb.toString();
    }
    
    public int getSize() {
        return size;
    }
}
